package com.sky.lightleafpicturepavilionserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.lightleafpicturepavilionserver.common.Result;
import com.sky.lightleafpicturepavilionserver.dto.WallpaperCategoryDTO;
import com.sky.lightleafpicturepavilionserver.dto.WallpaperCategoryDetailDTO;
import com.sky.lightleafpicturepavilionserver.dto.WallpaperCategoryPageDTO;
import com.sky.lightleafpicturepavilionserver.service.WallpaperCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 壁纸分类控制器
 */
@Tag(name = "壁纸分类管理", description = "壁纸分类相关接口")
@Slf4j
@RestController
@RequestMapping("/api/v1/wallpaper-category")
public class WallpaperCategoryController {

    @Resource
    private WallpaperCategoryService wallpaperCategoryService;

    /**
     * 获取启用的分类列表
     */
    @Operation(summary = "获取启用的分类列表", description = "获取所有启用状态的壁纸分类，用于首页展示")
    @GetMapping("/list")
    public Result<List<WallpaperCategoryDTO>> getCategoryList() {
        log.info("获取启用的分类列表");
        List<WallpaperCategoryDTO> categoryList = wallpaperCategoryService.getEnabledCategoryList();
        return Result.success(categoryList);
    }

    /**
     * 分页查询分类列表
     */
    @Operation(summary = "分页查询分类列表", description = "支持按名称搜索和状态筛选的分页查询")
    @GetMapping("/page")
    public Result<Page<WallpaperCategoryDTO>> getCategoryPage(WallpaperCategoryPageDTO pageDTO) {
        log.info("分页查询分类列表，参数：{}", pageDTO);
        Page<WallpaperCategoryDTO> categoryPage = wallpaperCategoryService.getCategoryPage(pageDTO);
        return Result.success(categoryPage);
    }

    /**
     * 获取分类详情
     */
    @Operation(summary = "获取分类详情", description = "根据分类ID获取分类详细信息")
    @GetMapping("/{categoryId}")
    public Result<WallpaperCategoryDetailDTO> getCategoryDetail(
            @Parameter(description = "分类ID") @PathVariable Long categoryId) {
        log.info("获取分类详情，分类ID：{}", categoryId);
        
        WallpaperCategoryDetailDTO categoryDetail = wallpaperCategoryService.getCategoryDetail(categoryId);
        if (categoryDetail == null) {
            return Result.error("分类不存在");
        }
        
        return Result.success(categoryDetail);
    }

    /**
     * 获取热门分类列表
     */
    @Operation(summary = "获取热门分类列表", description = "获取热门分类，按壁纸数量和排序权重排序")
    @GetMapping("/hot")
    public Result<List<WallpaperCategoryDTO>> getHotCategoryList(
            @Parameter(description = "限制数量，默认10") @RequestParam(defaultValue = "10") Integer limit) {
        log.info("获取热门分类列表，限制数量：{}", limit);
        List<WallpaperCategoryDTO> hotCategoryList = wallpaperCategoryService.getHotCategoryList(limit);
        return Result.success(hotCategoryList);
    }
}